import UIKit

/*
 Swift 的 String类型桥接到了基础库中的 NSString类。
 Foundation 同时也扩展了所有 NSString 定义的方法给 String 。
 也就是说，如果你导入 Foundation ，就可以在 String 中访问所有的 NSString  方法，无需转换格式。
 */

//多行字符串字面量

print("-----")

let quotation = """

The White Rabbit put on his spectacles.  "Where shall I begin,
please your Majesty?" he asked.
 
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."

"""
print("\(quotation)")
print("-----")
/*
当你的代码中在多行字符串字面量里包含了换行，那个换行符同样会成为字符串里的值。如果你想要使用换行符来让你的代码易读，却不想让换行符成为字符串的值，那就在那些行的末尾使用反斜杠（ \ ）
*/
let softWrappedQuotation = """
The White Rabbit put on his spectacles.  "Where shall I begin, \
please your Majesty?" he asked.
 
"Begin at the beginning," the King said gravely, "and go on \
till you come to the end; then stop."
"""
print(softWrappedQuotation)
print("-----")

var variableString = "Horse"
variableString += " and carriage"
//直接赋值于variableString
print(variableString)

//character字符类型
for zifu in "dog🐶!狗" {
    print(zifu)
}

//character 转 string
var catCharacter:[Character] = ["d","a","y","a"]
print(catCharacter)
let catString = String(catCharacter)
print(catString)


//string末尾追加character
let exclamationMark: Character = "i"

var welcomeString = "hello,lile"
welcomeString.append(exclamationMark)

//字符统计
let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪"
print("unusualMenagerie has \(unusualMenagerie.count) characters")


let countString = "count6"
print(countString.count)
print("startIndex:\(countString[countString.startIndex])")
let beforeIndex = countString.index(before: countString.endIndex)
print("beforIndex用法:\(countString[beforeIndex])")

let afterIndex = countString.index(after: countString.startIndex)
print("afterIndex用法:\(countString[afterIndex])")

print("endIndex:\(countString[countString.index(before: countString.endIndex)])")
//countString.endIndex  切记不能这么用  越界

let stringIndex = countString.index(countString.startIndex, offsetBy: 1)
print("offsetbBy后面的参数获取string的第几位元素(正序)：\(countString[stringIndex])")

//count属性为了确定字符串中的字符要遍历整个字符串的 Unicode 标量。
//count属性返回的字符统计并不会总是与包含相同字符的 NSString中 length属性相同。 NSString中的长度是基于在字符串的 UTF-16 表示中16位码元的数量来表示的，而不是字符串中 Unicode 扩展字形集群的数量。

//你可以在任何遵循了 Indexable 协议的类型中使用 startIndex 和 endIndex 属性以及 index(before:) ， index(after:) 和 index(_:offsetBy:) 方法。这包括这里使用的 String ，还有集合类型比如 Array ， Dictionary 和 Set 。

//插入和删除
//插入
var newWelcome = "hello"

newWelcome.insert(contentsOf: "!", at: newWelcome.endIndex)
//newWelcome.insert(contentsOf: "!", at: newWelcome.startIndex)
newWelcome.insert(contentsOf: " world", at: newWelcome.index(before: newWelcome.endIndex))

let newWelcomeIndex = newWelcome.index(newWelcome.startIndex, offsetBy: 5)
newWelcome[newWelcomeIndex]
newWelcome.insert(contentsOf: "JS", at: newWelcomeIndex)
newWelcome

//删除
//删除第一个
newWelcome.remove(at: newWelcome.startIndex)
newWelcome
//删除最后一个
newWelcome.remove(at: newWelcome.index(before: newWelcome.endIndex))
newWelcome
//删除指定下标字符
newWelcome.remove(at:newWelcome.index(newWelcome.startIndex, offsetBy: 4))
newWelcome
//删除区域范围
let subRange = newWelcome.index(newWelcome.endIndex, offsetBy: -5)..<newWelcome.endIndex
newWelcome.removeSubrange(subRange)

let startSubRange = newWelcome.startIndex...newWelcome.index(newWelcome.startIndex, offsetBy: 2)
newWelcome.removeSubrange(startSubRange)
//你可以在任何遵循了 RangeReplaceableIndexable 协议的类型中使用 insert(_:at:) ， insert(contentsOf:at:) ， remove(at:) 方法。这包括了这里使用的 String ，同样还有集合类型比如 Array ， Dictionary 和 Set 。
    
//子字符串
let greeting = "Hello, world!"
let index = greeting.index(greeting.startIndex, offsetBy: 5)
//subString 重用string的一部分内存  只要substring在用  那么原来的string就一直保存在内存里
//subString只适合短期保存  或是给另一个字符串赋值
let beginning = greeting[..<index]
let newGreeting = String(beginning)
//判断是否为空
if greeting.isEmpty {
    print("is empty")
}else{
    print("not empty")
}
//包含  区分大小写！
if greeting.contains("He"){
    print("contain H")
}else{
    print("not contains")
}

//前缀和后缀
let romeoAndJuliet = [
    "Act 1 Scene 1: Verona, A public place",
    "Act 1 Scene 2: Capulet's mansion",
    "Act 1 Scene 3: A room in Capulet's mansion",
    "Act 1 Scene 4: A street outside Capulet's mansion",
    "Act 1 Scene 5: The Great Hall in Capulet's mansion",
    "Act 2 Scene 1: Outside Capulet's mansion",
    "Act 2 Scene 2: Capulet's orchard",
    "Act 2 Scene 3: Outside Friar Lawrence's cell",
    "Act 2 Scene 4: A street in Verona",
    "Act 2 Scene 5: Capulet's mansion",
    "Act 2 Scene 6: Friar Lawrence's cell"
]
var PrefixCount = 0
for count in romeoAndJuliet {
    if count.hasPrefix("Act 1") {
        PrefixCount += 1
    }
}
print("romeoAndJuliet hasprefix Act 1 count's \(PrefixCount)")

var suffixCount = 0
var cellCount = 0
for count in romeoAndJuliet {
    if count.hasSuffix("Capulet's mansion") {
        suffixCount += 1
    }else if count.hasSuffix("Friar Lawrence's cell"){
        cellCount += 1
    }
}

print("\(suffixCount) mansion scenes;\(cellCount) cell scenes")

